<template>
  <div class="">
    <div class="tabs_form">
      <el-form :inline="true" class="selectLabel">
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="queryReq.dateTime"
            type="daterange"
            align="right"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="query">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabs_button">
      <el-button type="primary" @click="add" class="buttonLabel"
        >新建花销计划</el-button
      >
    </div>
    <div class="tabs_table">
      <el-table :data="tableData" border style="width: 100%" max-height="600">
        <el-table-column
          prop="serialNo"
          label="序号"
          align="center"
          type="index"
          width="60"
        >
        </el-table-column>
        <el-table-column
          prop="expensesType"
          label="类型"
          align="center"
          :formatter="formatterExpensesType"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="time"
          label="时间"
          align="center"
          :show-overflow-tooltip="true"
        ></el-table-column>
        <el-table-column
          prop="content"
          label="内容"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="estimatedAmountStr"
          label="预计金额"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          align="center"
          :formatter="formatterStatus"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="remark"
          label="备注"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="300" align="center">
          <template v-slot="scope">
            <el-button
              v-if="scope.row.status === 2"
              @click="finishExpenses(scope.$index, scope.row)"
              type="primary"
              plain
              >完成</el-button
            >
            <el-button
              @click="handleEdit(scope.$index, scope.row)"
              type="primary"
              plain
              >编辑</el-button
            >
            <el-button type="danger" @click="deleteData(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <DataEdit
    ref="dataEditRef"
    @dataEditConfig="dataEditConfig"
    :dataEditVisible="dataEditVisible"
    :isEdit="isEdit"
  />
  <FinishExpenses
    ref="finishExpensesRef"
    @finishExpensesConfig="finishExpensesConfig"
    :finishExpensesVisible="finishExpensesVisible"
  >
  </FinishExpenses>
</template>

<script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue'
import DataEdit from './dataEdit.vue'
import FinishExpenses from './finishExpenses.vue'
import { rows, queryPlan, deletePlan } from '@/api/expenses'
import { selectDetail } from '@/constants/commonSelect'
import { getGroupDetail } from '@/api/config'
import { getDateTime } from '@/components/util/dateTimeUtil'
import { ElMessage, ElMessageBox } from 'element-plus'

// 初始化查询
onMounted(() => {
  query()
  getExpensesType()
  queryReq.dateTime = getDateTime()
})

const dataEditRef = ref<any>(null)
let tableData = ref<rows[]>([])
const isEdit = ref<number>(0)
const dataEditVisible = ref<boolean>(false)
const finishExpensesRef = ref<any>(null)
const finishExpensesVisible = ref<boolean>(false)

let queryReq: any = reactive({
  dateTime: [''],
  startTime: '',
  endTime: '',
})

const expensesTypeParams = ref<selectDetail[]>([])

const getExpensesType = async () => {
  getGroupDetail('constgroup_expenses_type').then((res: any) => {
    expensesTypeParams.value = res
  })
}

const formatterExpensesType = (row: rows) => {
  let label = ''
  expensesTypeParams.value.forEach((item: any) => {
    if (item.value === row.expensesType) {
      label = item.label
    }
  })
  return label
}

const formatterStatus = (row: rows) => {
  let label = ''
  if (row.status === 2) {
    label = '未完成'
  } else if (row.status === 1) {
    label = '已完成'
  }
  return label
}

const query = () => {
  console.log('query')
  queryPlan(queryReq).then((res) => {
    tableData.value = res.data
  })
}

const add = () => {
  console.log('add')
  isEdit.value = 0
  dataEditVisible.value = true
}

const handleEdit = (index: number, row: any) => {
  console.log('handleEdit')
  dataEditVisible.value = true
  isEdit.value = 1
  // 调用子组件的 initsObj 方法
  dataEditRef.value?.initsObj(row)
}

const dataEditConfig = (status: any) => {
  console.log('dataEditConfig')
  dataEditVisible.value = status
  query()
}

const finishExpensesConfig = (status: any) => {
  console.log('finishExpensesConfig')
  finishExpensesVisible.value = status
  query()
}

const finishExpenses = (index: number, row: any) => {
  console.log('finishExpenses')
  finishExpensesVisible.value = true
  finishExpensesRef.value?.initsObj(row)
}

const deleteData = (row: rows) => {
  ElMessageBox.confirm('你确认删除这条数据吗?', 'Warning', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      deletePlan(row.id).then((res: any) => {
        if (res.data) {
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          query()
        } else {
          ElMessage({
            type: 'error',
            message: '删除失败',
          })
        }
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
</script>

<style lang="scss" scoped>
.tabs_form {
  height: 60px;
  margin-top: 10px;
  position: relative;
}

.tabs_table {
  position: relative;
  margin-top: 10px;
}

.tabs_select {
  top: 0px;
  width: 200px;
}

.tabs_button {
  position: absolute;
  left: 20px;
  top: 10px;
}

.selectLabel {
  position: absolute;
  right: 10px;
}

.buttonLabel {
  margin-left: 20px;
  margin-bottom: 40px;
}
</style>
